import { io } from 'socket.io-client'
import { getUser } from './user'

const fn = () => {}

export class Socket{

    constructor(){
        this.socketIo = null
    }

    connect(){
        const user = getUser()
        this.socketIo = io('ws://localhost:1080',{
            transports: ["websocket"],
            query:{
                user_name: user.name,
                user_id: user.id
            }
        })

        this.socketIo.on('connect',function(){
            console.log('连接成功');
            this.connecting = true
        })

        this.socketIo.on('disconnect',function(){
            console.log('disconnect');
        })
    }

    close(){
        this.socketIo.close()
    }

    on(eventName,callBack){
        this.socketIo.on(eventName,callBack)
    }

    emit(eventName,msg='',callBack = fn){
        try {
            this.socketIo.emit(eventName,msg,callBack)
        } catch (error) {
            console.log(error);
        }
    }
}